package com.hongmeng.teamwork.entity.base;

import com.hongmeng.teamwork.enums.ResultCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {

    private Boolean success;
    private Integer code;
    private String message;
    private T data;

    public static Result<?> ok() {
        return Result.builder()
                .success(true)
                .code(ResultCodeEnum.SUCCESS.getCode())
                .message(ResultCodeEnum.SUCCESS.getDesc())
                .build();
    }

    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        result.setSuccess(true);
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getDesc());
        result.setData(data);
        return result;
    }

    public static Result<?> fail() {
        return Result.builder()
                .success(false)
                .code(ResultCodeEnum.FAILURE.getCode())
                .message(ResultCodeEnum.FAILURE.getDesc())
                .build();
    }

    public static <T> Result<T> fail(T data) {
        Result<T> result = new Result<>();
        result.setSuccess(false);
        result.setCode(ResultCodeEnum.FAILURE.getCode());
        result.setMessage(ResultCodeEnum.FAILURE.getDesc());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> fail(Integer code, String message) {
        Result<T> result = new Result<>();
        result.setSuccess(false);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
}
